package YZ.Students.Mier.controller; /**
 * @className: ${NAME}
 * @author: shuishui
 * @date: 2022/12/02 17:18
 */
import YZ.Students.Mier.Listener.ThymeleafListener;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.WebConnection;
import java.io.IOException;

@WebServlet("/PageServlet/*")
public class PageServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String contextPath = req.getContextPath();
        String path = req.getRequestURI().replaceAll(contextPath+"/PageServlet/", "");
        String page = path.replace(".html", "");
        System.out.println("page:"+page);
        ServletContext servletContext = req.getServletContext();
        TemplateEngine templateEngine = ThymeleafListener.getTemplateEngine(servletContext);
        WebContext webContext = new WebContext(req, resp, servletContext);
        templateEngine.process(page, webContext, resp.getWriter());
    }
}
